class SessionController < ApplicationController
  def new
    
  end
  
  def create
    password_authentication
  end
  
  def logout
    reset_session
    redirect_to new_session_url
  end

  protected
  
  def password_authentication
    if(params[:user] && (@logged_user = User.authenticate(params[:user][:email], params[:user][:password])))
      successful_authentication
    else
      failed_authentication "Invalid email and/or password!"
    end
  end
  
  private
  
  def successful_authentication
    session[:user_id] = @logged_user.id
    
    redirect_to projects_url
  end
  
  def failed_authentication(message)
    flash[:error] = message
    redirect_to new_session_url
  end
  
  def get_logged_user
    return true
  end
end
